package edu.ynu.se.xiecheng.achitectureclass.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import edu.ynu.se.xiecheng.achitectureclass.entity.Item;
import edu.ynu.se.xiecheng.achitectureclass.entity.Shop;
import edu.ynu.se.xiecheng.achitectureclass.entity.User;
import edu.ynu.se.xiecheng.achitectureclass.entity.vo.Result;
import edu.ynu.se.xiecheng.achitectureclass.mapper.ItemMapper;
import edu.ynu.se.xiecheng.achitectureclass.mapper.UserMapper;
import edu.ynu.se.xiecheng.achitectureclass.service.ItemService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author laruui
 * @since 2023-12-07
 */
@Slf4j
@Service
public class ItemServiceImpl extends ServiceImpl<ItemMapper, Item> implements ItemService {

    @Resource
    private ItemMapper itemMapper;

    @Resource
    private UserMapper userMapper;

    @Override
    public Result pageItem(int current, int size, String userId, String shopId) {
        try {
            Page<Item> itemPage = new Page<>(current, size);
            Page<Item> selectPage = new Page<>();
            LambdaQueryWrapper<Item> itemLambdaQueryWrapper;
            // 获取用户id
            // 假定是客户
            String flag = userMapper.selectOne(new LambdaQueryWrapper<User>().eq(User::getId, userId)).getFlag();
            // 判断登陆用户类型到底是什么
            if ("customer".equals(flag)) {
                itemLambdaQueryWrapper = new LambdaQueryWrapper<>();
                itemLambdaQueryWrapper.eq(Item::getUpordown, "up");
                if (shopId != null) {
                    itemLambdaQueryWrapper.eq(Item::getShopId, shopId);
                }
                // 是客户
                // 进入店铺获取到的应该是店铺上线了的商品
                selectPage = itemMapper.selectPage(itemPage, itemLambdaQueryWrapper);
            } else if ("business".equals(flag)) {
                // 是商家
                // 进入店铺获取到的应该是店铺全部的商品
                itemLambdaQueryWrapper = new LambdaQueryWrapper<>();
                if (shopId != null) {
                    itemLambdaQueryWrapper.eq(Item::getShopId, shopId);
                }
                if (userId != null) {
                    itemLambdaQueryWrapper.eq(Item::getUserId, userId);
                }
                selectPage = itemMapper.selectPage(itemPage, itemLambdaQueryWrapper);
            }
            return Result.builder().code(200).message("查询成功！").data(selectPage).build();
        } catch (Exception e) {
            log.info("出现异常！");
            return Result.builder().code(500).message("系统故障").build();
        }
    }

    @Override
    public Result addNewItem(Item item) {
        try {
            item.setUpordown("down");
            int row = itemMapper.insert(item);
            return Result.builder().code(200).message(String.valueOf(row)).build();
        } catch (Exception e) {
            log.info("出现异常！");
            return Result.builder().code(500).message("系统故障").build();
        }
    }

    @Override
    public Result updateUpOrDown(String itemId, String flag) {
        try {
            Item item = itemMapper.selectOne(new LambdaQueryWrapper<Item>().eq(Item::getId, itemId));
            if ("down".equals(item.getUpordown())) {
                item.setUpordown("up");
            } else if ("up".equals(item.getUpordown())) {
                item.setUpordown("down");
            }
            int row = itemMapper.updateById(item);
            return Result.builder().code(200).message(String.valueOf(row)).build();
        } catch (Exception e) {
            log.info("出现异常！");
            return Result.builder().code(500).message("系统故障").build();
        }
    }
}
